(function(payload){

    function checkClass(obj, type){
        return (typeof obj) === type;
    }
    function Paramters(params){
        return {
            get: function(key){
                var val = this.getArray(key);
                return util.getArrayDefault(val, 0, null);
            },
            getArray: function(key){
                return util.getArrayDefault(params, key, []);
            }
        }
    }
    function getParameter(url){
        var KVOBJ = {};
        if (!url){
            url = location.href;
        }
        var search = url;
        var index = search.indexOf("?");
        if ( index === -1){
            return new Paramters({});
        }
        var index2 = search.indexOf("#");
        if (index2 == -1) {
            index2 = search.length;
        }
        else if (index2 <= index) {
            // #在？之前置空
            index2 = index + 1;
        }
        search = search.substring(index + 1, index2);
        var kvs = search.split("&");
        if (kvs.length > 0){
            for (var i = 0, l = kvs.length; i < l; i ++){
                var index = kvs[i].indexOf("=");
                var key = kvs[i];
                var value = "";
                if (-1 != index){
                    key = kvs[i].substring(0, index);
                    if (index < kvs[i].length){
                        value = kvs[i].substring(index + 1);
                    }
                }
                var VOBJ = util.getArrayDefault(KVOBJ, key, []);
                value = decodeURI(value);
                VOBJ.push(value);
                KVOBJ[key] = VOBJ;
            }
        }
        return new Paramters(KVOBJ);
    }
    var os = function (){
        var ua = navigator.userAgent,
            isWindowsPhone = /(?:Windows Phone)/.test(ua),
            isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
            isAndroid = /(?:Android)/.test(ua),
            isFireFox = /(?:Firefox)/.test(ua),
            isChrome = /(?:Chrome|CriOS)/.test(ua),
            isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
            isPhone = /(?:iPhone)/.test(ua) && !isTablet,
            isPc = !isPhone && !isAndroid && !isSymbian;
        return {
            isTablet: isTablet,
            isPhone: isPhone,
            isAndroid: isAndroid,
            isPc: isPc
        };
    }();
    function Counter(func, conditionCount){
        var count = 0;
        var lastClickTime = 0;
        var condition = conditionCount;
        var delay = 5000;
        this.count = function(){
            let current =  new Date().getTime();
            if (current - lastClickTime > delay){
                count = 0;
            }
            count ++;
            lastClickTime = current;
            if (count >= condition){
                func(count, condition);
            }
        }
        this.setDelay = function(delayTime){
            delay = delayTime;
        }
        this.clear = function(){
            count = 0;
        }
    }
    var util = {
        counter: function(func, condition){
            return new Counter(func, condition);
        },
        createTag: function(tag, className){
            var box = document.createElement(tag);
            if (!util.isNullString(className)) {
                box.setAttribute("class", className);
            }
            return box;
        },
        isNumber: function(obj){
            return checkClass(obj, "number");
        },
        isString: function(obj){
            return checkClass(obj, "string");
        },
        isNullString: function(str, plus){
            var r = undefined === str || null === str || str === "";
            if (plus){
                r = r || "undefined" == str || "null" === str;
            }
            return r;
        },
        isEmptyArray: function(array){
            return undefined === array || null === array || array.length === 0;
        },
        clone: function(obj){
            return JSON.parse(JSON.stringify(obj));
        },
        merge: function(target, source){
            let rst = util.clone(target);
            for (var key in source){
                rst[key] = source[key];
            }
            return rst;
        },
        getDefault: function(value, defaultValue, plus) {
            if (util.isNullString(value, plus)) {
                return defaultValue;
            }
            return value;
        },
        getArrayDefault: function(array, index , defaultValue){
            array = util.getDefault(array, []);
            if ( util.isNumber(index) ){
                if (array.length <= index){
                    return defaultValue;
                }
            }
            else if (!util.isString(index)){
                return defaultValue;
            }
            return util.getDefault(array[index], defaultValue);
        },
        setItem: function(key, value){
            localStorage.setItem(key, value);
        },
        getItem: function(key){
            return localStorage.getItem(key);
        },
        removeItem: function(key){
            localStorage.removeItem(key);
        },
        getParameter: getParameter,
        os: os,
        redirect: function(path){
            console.log(path);
            if (path.startsWith("/")){
                location.href = location.origin + path;
            }
            else if (path.startsWith("http")) {
                location.href = path;
            }
            else{
                console.error("暂不支持");
            }
        },
        open: function (path){
            var a = document.getElementById("openRandoms852352313");
            if(util.isNullString(a) ) {
                a = util.createTag("a");
                a.setAttribute("target", "_blank");
                a.setAttribute("style", "display:none;");
                a.setAttribute("id", "openRandoms852352313");
                a.innerHTML = "<span>open</span>";
                document.body.appendChild(a);
            }
            if (path.startsWith("//")){
                path = location.protocol + path;
            }
            else if (path.startsWith("/")){
                path = location.origin + path;
            }
            a.setAttribute("href", path);
            // 防止反复添加
            a.getElementsByTagName("span")[0].click();
        }
    }
    window.util = util;
})(window);
